from django.db import models


# class ExtendSalesman(models.Model):
#     user_type = models.ForeignKey("UserType", verbose_name="用户类型")
#     sales = models.OneToOneField("sales_man.SalesMan", verbose_name="业务员")
#
#     class Meta:
#         verbose_name = "业务员扩展表"
#         verbose_name_plural = verbose_name
#
#     def __str__(self):
#         return self.user_type.get_account_type_display()
#
#
# class MenuPermissionsManager(models.Manager):
#     def get_menus(self, salesman):
#         return self.filter(user_type__salesman=salesman, menu__show_status=True)
#
#
# class MenuPermissions(models.Model):
#     """
#     菜单权限
#     """
#     user_type = models.ForeignKey("UserType", verbose_name="管理员类型")
#     menu = models.ForeignKey("Menus", verbose_name="菜单")
#     objects = MenuPermissionsManager()
#
#     class Meta:
#         verbose_name = "菜单权限"
#         verbose_name_plural = verbose_name
#
#     def __str__(self):
#         return self.user_type.get_account_type_display()


class ExtendSalesman(models.Model):
    user_type = models.ForeignKey("UserType", verbose_name="用户类型")
    sales = models.OneToOneField("sales_man.SalesMan", verbose_name="业务员")

    class Meta:
        verbose_name = "业务员扩展表"
        verbose_name_plural = verbose_name

    def __str__(self):
        if self.sales.account:
            return "%s:%s" % (self.sales.account, self.user_type.get_account_type_display())
        else:
            return "%s:%s" % (self.sales.id, self.user_type.get_account_type_display())


class Menus(models.Model):
    menu = models.CharField(verbose_name="菜单", max_length=16)
    url = models.CharField(verbose_name="地址", max_length=64, blank=True, null=True)
    parent_menus = models.ForeignKey("self", verbose_name="父菜单", blank=True, null=True)
    open_menus = models.ForeignKey("self", verbose_name="展开菜单", related_name="open_set", blank=True, null=True)
    show_status = models.BooleanField(verbose_name="是否展示到菜单上", default=True)

    class Meta:
        verbose_name = "后台菜单"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.menu


class UserType(models.Model):
    AccountType = ((1, "超级管理员"), (2, "渠道管理员"))
    account_type = models.IntegerField(choices=AccountType, default=2, db_index=True, verbose_name="管理员类型")
    menus = models.ManyToManyField("Menus", verbose_name="菜单")

    class Meta:
        verbose_name = "管理员类型"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.get_account_type_display()
